package com.tanhua.dubbo.server.api;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.common.enums.PassType;
import com.tanhua.common.enums.TopState;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.dubbo.server.pojo.Video;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class ManagerApiImpl implements ManageApi {


    @Autowired
    private QuanZiApi quanZiApi;

    @Autowired
    private VideoApi videoApi;


    @Autowired
    private MongoTemplate mongoTemplate;

    /*
     *   消息通过
     * */
    @Override
    public Boolean messagePass(String[] ids) {
        try {
            //首先判断传入的id是动态id，还是视频id
            for (String id : ids) {
                //根据id找到对应的动态表信息
                //如果能够找到，代表是动态id
                Publish publish = quanZiApi.queryPublishById(id);
                if (ObjectUtil.isNotEmpty(publish)) {
                    //修改动态表中id的字段信息
                    Query query = Query.query(Criteria.where("id").is(new ObjectId(id)));
                    Update update = new Update();
                    update.set("messageType", PassType.PERSONPASS.getValue());
                    mongoTemplate.upsert(query, update, Publish.class);
                } else {
                    //如果找不到，判断是否是视频id
                    Video video = videoApi.queryVideoById(id);
                    if (ObjectUtil.isNotEmpty(video)) {
                        Query query = Query.query(Criteria.where("id").is(new ObjectId(id)));
                        Update update = new Update();
                        update.set("messageType", PassType.PERSONPASS.getValue());
                        mongoTemplate.upsert(query, update, Video.class);
                    } else {
                        //TODO 可能出现的情况
                    }
                }
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /*
     *   消息审核不过
     * */
    @Override
    public Boolean rejectMessage(String[] ids) {
        try {
            //首先判断传入的id是动态id，还是视频id
            for (String id : ids) {
                //根据id找到对应的动态表信息
                //如果能够找到，代表是动态id
                Publish publish = quanZiApi.queryPublishById(id);
                if (ObjectUtil.isNotEmpty(publish)) {
                    //修改动态id的字段信息
                    Query query = Query.query(Criteria.where("id").is(new ObjectId(id)));
                    Update update = new Update();
                    update.set("messageType", PassType.PERSONREFUSE.getValue());
                    mongoTemplate.upsert(query, update, Publish.class);
                } else {
                    //如果找不到，判断是否是视频id
                    Video video = videoApi.queryVideoById(id);
                    if (ObjectUtil.isNotEmpty(video)) {
                        Query query = Query.query(Criteria.where("id").is(new ObjectId(id)));
                        Update update = new Update();
                        update.set("messageType", PassType.PERSONREFUSE.getValue());
                        mongoTemplate.upsert(query, update, Video.class);
                    } else {
                        //TODO 可能出现的情况
                    }
                }
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /*
     *   消息审核撤销
     * */
    @Override
    public Boolean revocationMessage(String[] ids) {
        try {
            //首先判断传入的id是动态id，还是视频id
            for (String id : ids) {
                //根据id找到对应的动态表信息
                //如果能够找到，代表是动态id
                Publish publish = quanZiApi.queryPublishById(id);
                if (ObjectUtil.isNotEmpty(publish)) {
                    //修改动态id的字段信息
                    Query query = Query.query(Criteria.where("id").is(new ObjectId(id)));
                    Update update = new Update();
                    update.set("messageType", PassType.WAIT.getValue());
                    mongoTemplate.upsert(query, update, Publish.class);
                } else {
                    //如果找不到，判断是否是视频id
                    Video video = videoApi.queryVideoById(id);
                    if (ObjectUtil.isNotEmpty(video)) {
                        Query query = Query.query(Criteria.where("id").is(new ObjectId(id)));
                        Update update = new Update();
                        update.set("messageType", PassType.WAIT.getValue());
                        mongoTemplate.upsert(query, update, Video.class);
                    } else {
                        //TODO 可能出现的情况
                    }
                }
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /*
     *   消息置顶
     * */
    @Override
    public Boolean topMessage(String id) {
        //首先判断传入的id是动态id，还是视频id
        //根据id找到对应的动态表信息
        //如果能够找到，代表是动态id
        Publish publish = quanZiApi.queryPublishById(id);
        if (ObjectUtil.isNotEmpty(publish)) {
            //修改动态id的字段信息
            Query query = Query.query(Criteria.where("id").is(new ObjectId(id)));
            Update update = new Update();
            update.set("topState", TopState.Top.getValue());
            mongoTemplate.upsert(query, update, Publish.class);
        }
        return true;
    }


    /*
     *   查询所有的动态信息
     * */
    @Override
    public List<Publish> findAllPublish(Integer page, Integer pageSize, String sortProp, String sortOrder, String sd, String ed, String id, String state,String uid) {
        Query query = new Query();

        //判断传入得uid是否为null
        if (ObjectUtil.isNotNull(uid)){
            //根据userId查询对应的用户信息
            query.addCriteria(Criteria.where("userId").is(Convert.toLong(uid)));
        }

        //获取审核状态
        if(ObjectUtil.isNotNull(state)){
            //审核状态为0，查询所有
            if (state.equals("3") ){
                query.addCriteria(Criteria.where("messageType").is(1));
            }
            if (state .equals("4") ){
                query.addCriteria(Criteria.where("messageType").is(5));
            }
            if (state.equals("5") ){
                query.addCriteria(Criteria.where("messageType").is(4));
            }
        }

        //根据时间排序
        if (ObjectUtil.isNotNull(sd) && ObjectUtil.isNotNull(ed)){
            //开始时间和结束时间
            if (!("-1").equals(sd)  && !("-1").equals(ed) ) {
                query.addCriteria(Criteria.where("created").gte(Convert.toLong(sd)).lte(Convert.toLong(ed)));
            }
        }
        //分页排序
        if (ObjectUtil.isAllNotEmpty(page, pageSize)) {
            PageRequest pageRequest = PageRequest.of(page -1, pageSize);
            query.with(pageRequest);
        }
        //根据传入id查询
       if (ObjectUtil.isNotNull(id)){
           if (ObjectUtil.isNotEmpty(id) ) {
               query.addCriteria(Criteria.where("userId").is(Convert.toLong(id)));
           }
       }
        List<Publish> publishList = mongoTemplate.find(query, Publish.class);

        return publishList;
    }

    //查询所有动态的总页数
    public Integer findAllPublishCount() {
        List<Publish> list = mongoTemplate.findAll(Publish.class);

        return list.size();
    }

    /*
     *   取消消息置顶
     * */
    @Override
    public Boolean untopMessage(String id) {
        //首先判断传入的id是动态id，还是视频id
        //根据id找到对应的动态表信息
        //如果能够找到，代表是动态id
        Publish publish = quanZiApi.queryPublishById(id);
        if (ObjectUtil.isNotEmpty(publish)) {
            //修改动态id的字段信息
            Query query = Query.query(Criteria.where("id").is(new ObjectId(id)));
            Update update = new Update();
            update.set("topState", TopState.up.getValue());
            mongoTemplate.upsert(query, update, Publish.class);
        }
        return true;
    }

    /*
     *   消息详情
     * */
    @Override
    public Publish getMessage(String id) {
        return quanZiApi.queryPublishById(id);
    }

    /*
     *   根据传入得total头信息，查询对应的数量
     * */
    @Override
    public Integer find(String id,String sd,String ed,Integer value,String uid) {
        Query query = new Query();
        if (ObjectUtil.isNotNull(sd) && ObjectUtil.isNotNull(ed)){
            //开始时间和结束时间
            if (!("-1").equals(sd)  && !("-1").equals(ed) ) {
                query.addCriteria(Criteria.where("created").gte(Convert.toLong(sd)).lte(Convert.toLong(ed)));
            }
        }
        //根据传入id查询
        if (ObjectUtil.isNotNull(id)){
            if (ObjectUtil.isNotEmpty(id) ) {
                query.addCriteria(Criteria.where("userId").is(Convert.toLong(id)));
            }
        }
        //根据传入得total信息，查询状态
        if (ObjectUtil.isNotNull(value)) {
            query.addCriteria(Criteria.where("messageType").is(value));
        }

        //判断传入得uid是否为null
        if (ObjectUtil.isNotNull(uid)){
            //根据userId查询对应的用户信息
            query.addCriteria(Criteria.where("userId").is(Convert.toLong(uid)));
        }
        long count = mongoTemplate.count(query, Publish.class);
        return Convert.toInt(count);
    }

}
